import tkinter as tk
from tkinter import messagebox
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pickle

# 生成的测试用例
test_cases = [
    ("虚无之地", "张三"),
    ("星际漫游", "李四"),
    ("平凡的世界", "路遥"),
    ("时间简史", "史蒂芬·霍金"),
    ("未来边界", "王五"),
    ("银河帝国", "艾萨克·阿西莫夫"),
    ("永恒的终结", "艾萨克·阿西莫夫"),
    ("编程之美", "刘畅"),
    ("代码大全", "史蒂夫·迈克康奈尔"),
    ("大数据时代", "涂子沛"),
    ("深度学习", "伊恩·古德费洛"),
    ("机器学习", "周志华"),
    ("数据挖掘", "黄成"),
    ("云计算原理", "李明"),
    ("人工智能", "约翰·麦卡锡"),
    ("计算机网络", "詹姆斯·库罗西"),
    ("操作系统", "安德鲁·塔能鲍姆"),
    ("编译原理", "阿尔弗雷德·艾侯"),
    ("软件工程", "罗杰·S.普莱斯曼"),
    ("算法导论", "托马斯·H.科尔曼"),
]

results = []

def integrate_test(book_name, writer):
    global results
    # 设置浏览器选项
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    options.add_argument("disable-blink-features=AutomationControlled")
    options.add_argument(
        'User-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36')

    # 初始化浏览器
    driver = webdriver.Chrome(options=options)

    # 打开网站
    driver.get("https://www.kongfz.com")

    # 加载并添加cookie
    with open('../cookies.pkl', 'rb') as f:
        cookies = pickle.load(f)
        for cookie in cookies:
            if 'sameSite' in cookie:
                del cookie['sameSite']
            driver.add_cookie(cookie)

    driver.refresh()

    # 点击高级搜索按钮
    search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//a[@class='adv-search-btn' and text()='高级搜索']")))
    search.click()

    # 获取当前窗口句柄
    current_window = driver.current_window_handle

    # 更换窗口句柄到第二个窗口
    for window_handle in driver.window_handles:
        if window_handle != current_window:
            driver.switch_to.window(window_handle)
            break

    # 填写搜索条件
    book_box = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.NAME, "itemname")))
    book_box.send_keys(book_name)
    writer_box = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.NAME, "author")))
    writer_box.send_keys(writer)

    # 点击搜索按钮
    searchbtn = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "submitBtn")))
    searchbtn.click()

    # 更换窗口句柄到第三个窗口
    for window_handle in driver.window_handles:
        if window_handle != current_window and window_handle != driver.current_window_handle:
            driver.switch_to.window(window_handle)
            break

    # 等待搜索结果加载
    try:
        WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME, "product-item-wrap")))
        results.append(f'搜索成功：{book_name} - {writer}')
    except:
        results.append(f'搜索无结果：{book_name} - {writer}')

    driver.quit()

def start_integration_tests():
    for book_name, writer in test_cases:
        integrate_test(book_name, writer)
    messagebox.showinfo('测试结果', '\n'.join(results))

start_integration_tests()